/**
 * 
 */
package com.ibm.testbed.script.wizards;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;

/**
 * @author administrator
 *
 */
public class TestbedScriptExportWizard extends Wizard implements IExportWizard {

	
	private TestbedScriptExportPage page;
	private IStructuredSelection selection;
	/**
	 * 
	 */
	public TestbedScriptExportWizard() {
		super();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
	 */
	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = getValidSelection();
		setWindowTitle("Export Testbed Script");
	}

	public void addPages(){
		page=new TestbedScriptExportPage("Export Testbed Script");
		page.init(selection);
		addPage(page);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public boolean performFinish() {
		
		
		page.finishedPage();
		
		return true;
	}

	public static IStructuredSelection getValidSelection() {
		org.eclipse.jface.viewers.ISelection currentSelection = JavaPlugin
				.getActiveWorkbenchWindow().getSelectionService()
				.getSelection();
		if (currentSelection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
			List selectedElements = new ArrayList(structuredSelection.size());
			for (Iterator iter = structuredSelection.iterator(); iter.hasNext();) {
				Object selectedElement = iter.next();
				if (selectedElement instanceof IProject)
					addProject(selectedElements, (IProject) selectedElement);
				else if (selectedElement instanceof IResource)
					addResource(selectedElements, (IResource) selectedElement);
				else if (selectedElement instanceof IJavaElement)
					addJavaElement(selectedElements,
							(IJavaElement) selectedElement);
			}

			return new StructuredSelection(selectedElements);
		} else {
			return StructuredSelection.EMPTY;
		}
	}
	
	public static void addProject(List selectedElements, IProject project) {
		try {
			if (project.hasNature("org.eclipse.jdt.core.javanature"))
				selectedElements.add(JavaCore.create(project));
		} catch (CoreException ce) {
		}
	}

	public static void addResource(List selectedElements, IResource resource) {
		IJavaElement je = JavaCore.create(resource);
		if (je != null && je.exists() && je.getElementType() == 5)
			selectedElements.add(je);
		else
			selectedElements.add(resource);
	}
	
	public static void addJavaElement(List selectedElements, IJavaElement je) {
		if (je.getElementType() == 5)
			selectedElements.add(je);
		else if (je.getElementType() == 6)
			selectedElements.add(je);
		else if (je.getElementType() == 2)
			selectedElements.add(je);
		else if (je.getElementType() == 4) {
			if (!isInArchiveOrExternal(je))
				selectedElements.add(je);
		} else if (je.getElementType() == 3) {
			if (!isInArchiveOrExternal(je))
				selectedElements.add(je);
		} else {
			org.eclipse.jdt.core.IOpenable openable = je.getOpenable();
			if (openable instanceof ICompilationUnit)
				selectedElements
						.add(((ICompilationUnit) openable).getPrimary());
			else if ((openable instanceof IClassFile)
					&& !isInArchiveOrExternal(je))
				selectedElements.add(openable);
		}
	}
	
	private static boolean isInArchiveOrExternal(IJavaElement element) {
		IPackageFragmentRoot root = JavaModelUtil
				.getPackageFragmentRoot(element);
		return root != null && (root.isArchive() || root.isExternal());
	}
}
